﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using BLL;
using DAL;
using System.Text;

public partial class Public_Default : System.Web.UI.Page
{
    String StrConn = WebConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    LoginHistoryHandler objLoginHistoryHandler = new LoginHistoryHandler();
    string UserId = "";
    int level;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["count" + UserId] = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        UsersHandler objUsersHandler = new UsersHandler();
        List<Users> lstUsers = objUsersHandler.GetUsersList(StrConn);
        Users objUsers = new Users();
        string FullName = "";
        string idUser = id.Text.ToString();
        string passUser = password.Text.ToString();

        int check = 0;
        if (lstUsers.Count > 0)
        {
            foreach (Users obj in lstUsers)
            {
                string iid = obj.UserName.ToString();
                string pass = obj.Password.ToString();

                if (idUser.Equals(iid) && passUser.Equals(pass))
                {
                    check = 1;
                    UserId = obj.UserID.ToString();
                    FullName = obj.FullName.ToString();
                    level = Int16.Parse(obj.Level.ToString());

                }
            }
        }

        if (check == 1)
        {
            LoginHistory objLoginHistory = new LoginHistory();
            Session["FullName"] = FullName;
            Session["UserId"] = UserId;
            string SessionID = Session.SessionID.ToString();
            Session["SessionID"] = SessionID;
            objLoginHistory.UserID = Convert.ToInt32(UserId);
            objLoginHistory.LoginDate = DateTime.Now.ToString();
            Session["LoginDate"] = objLoginHistory.LoginDate;
            Session["Level"] = level;
            objLoginHistory.LogoutDate = DateTime.Now.ToString();
            objLoginHistory.Descriptions = "";
            objLoginHistory.SessionId = SessionID;
            objLoginHistoryHandler.AddNewLoginHistory(objLoginHistory, StrConn);
            if (level == 1)
            {
                Response.Redirect("~/Public/Home.aspx");
            }
            else
            {
                Response.Redirect("Home.aspx");
            }

        }
        else
        {
            Session["count" + UserId] = Int16.Parse(Session["count" + UserId].ToString()) + 1;
            this.msg.Text = "Incorrect username or password.";
        }
        if (!idUser.Equals(""))
        {
            Users obj = objUsersHandler.GetUsersDetailsWithUserName(idUser, StrConn);
            if (obj != null)
            {
                if (idUser.Equals(obj.UserName) && Int16.Parse(Session["count" + UserId].ToString()) > 3)
                {
                    obj.Block = "0";
                    obj.Isactive = "1";
                    obj.Updated = DateTime.Now.ToString();
                    objUsersHandler.UpdateUsers(obj, StrConn);
                    Session.Remove("count" + UserId);
                }
            }

        }
        
    }
}